package cn.client.ui.view.main.user.avoidancePain

import android.widget.Toast
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Star
import androidx.compose.material.icons.filled.StarOutline
import androidx.compose.material3.Icon
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import cn.client.data.userStateManager
import cn.client.ui.components.BaseTopBar
import cn.client.ui.view.main.mindfulness.HomeSection
import cn.client.ui.view.main.mindfulness.RatingBar
import kotlinx.coroutines.launch


// 痛苦回避事项页面
@Composable
fun AvoidancePainPage(
    id: Int,
    navController: NavController,
    viewModel: AvoidancePainViewModel = hiltViewModel()
) {

    val avoidancePainInfoState = viewModel.avoidancePainInfo.collectAsState(initial = null)
    val avoidancePainInfo = avoidancePainInfoState.value

    val context = LocalContext.current
    val coroutineScope = rememberCoroutineScope()

    coroutineScope.launch {
        viewModel.dispatch(AvoidancePainViewModel.PageIntent.SelectInfo(id = id))
    }

    LaunchedEffect(Unit) {
        viewModel.pageEvents.collect {
            when (it) {
                is AvoidancePainViewModel.PageEvent.Error -> {
                    Toast.makeText(context, "提示：${it.error}", Toast.LENGTH_SHORT).show()
                }
                else -> Unit
            }
        }
    }

    Scaffold(
        topBar = {
            BaseTopBar(
                title = "痛苦回避事项",
                onClickArrowBack = {
                    coroutineScope.launch {
                        navController.navigateUp()
                    }
                }
            )
        }
    ) {
        Column(
            modifier = Modifier.padding(it)
        ) {
            if (avoidancePainInfo != null) {
                HomeSection(title = "情景") {
                    Row(
                        modifier = Modifier.padding(15.dp)
                    ) {
                        Text(text = avoidancePainInfo.scene)
                    }
                }
                HomeSection(title = "回避程度") {
                    Row(
                        modifier = Modifier.padding(15.dp)
                    ) {
                        // 显示打分条
                        AvoidancePainRatingBar(
                            currentRating = avoidancePainInfo.avoidanceLevel
                        )
                    }
                }
                HomeSection(title = "痛苦程度") {
                    Row(
                        modifier = Modifier.padding(15.dp)
                    ) {
                        // 显示打分条
                        AvoidancePainRatingBar(
                            currentRating = avoidancePainInfo.painLevel
                        )
                    }
                }
            }
        }
    }
}


// 痛苦回避程度可组合项
@Composable
fun AvoidancePainRatingBar(
    maxRating: Int = 10,
    currentRating: Int,
    starsColor: Color = Color.Magenta
){
    Row {
        for (i in 1..maxRating) {
            Icon(
                imageVector = if (i <= currentRating) Icons.Filled.Star
                else Icons.Filled.StarOutline,
                contentDescription = null,
                tint = if (i <= currentRating) starsColor
                else Color.Unspecified,
                modifier = Modifier
                    .padding(4.dp)
            )
        }
    }
}